// eslint-disable-next-line import/no-extraneous-dependencies
import { parse } from 'url';

// mock userDataSource
const genList = (current, pageSize) => {
  const userDataSource = [];

  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    userDataSource.push({
      key: index,
      mobile: `手机号 ${index}`,
      password: `密码 ${index}`,
      userName: `用户昵称 ${index}`,
      roleId: 1,
      roleName: `角色名称1`,
      storeId:1,
      storeName: `商户名称1`,
      status: Math.floor(Math.random() * 10) % 2,
    });
  }

  userDataSource.reverse();
  return userDataSource;
};

let userDataSource = genList(1, 100);

function getUser(req, res, u) {
  let realUrl = u;

  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }

  const { current = 1, pageSize = 10 } = req.query;
  const params = parse(realUrl, true).query;
  let dataSource = [...userDataSource].slice((current - 1) * pageSize, current * pageSize);

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }

      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(item => {
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
            return true;
          }

          return false;
        }),
      );
    });
    dataSource = filterDataSource;
  }

  if (params.userName) {
    dataSource = dataSource.filter(data => data.userName.includes(params.userName || ''));
  }
  if (params.mobile) {
    dataSource = dataSource.filter(data => data.mobile.includes(params.mobile || ''));
  }

  const result = {
    data: dataSource,
    total: userDataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };
  return res.json(result);
}

function postUser(req, res, u, b) {
  let realUrl = u;

  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }

  const body = (b && b.body) || req.body;
  const { method, userName, mobile,password,roleId,storeId,status, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      userDataSource = userDataSource.filter(item => key !== item.key);
      break;

    case 'post':
      (() => {
        const i = Math.ceil(Math.random() * 10000);
        const newUser = {
          key: userDataSource.length,
          userName, mobile,password,roleId,storeId,status:1
        };
        userDataSource.unshift(newUser);
        return res.json(newUser);
      })();

      return;

    case 'update':
      (() => {
        let newUser = {};
        userDataSource = userDataSource.map(item => {
          if (item.key === key) {
            newUser = { ...item, userName, mobile,password,roleId,storeId,status };
            return { ...item, userName, mobile,password,roleId,storeId,status };
          }

          return item;
        });
        return res.json(newUser);
      })();

      return;

    default:
      break;
  }

  const result = {
    list: userDataSource,
    pagination: {
      total: userDataSource.length,
    },
  };
  res.json(result);
}

export default {
  'GET /api/user': getUser,
  'POST /api/user': postUser,
  'GET /api/role': {data:[
    {
      roleId: 1,
      roleName: '角色名称1',
    },
    {
      roleId: 2,
      roleName: '角色名称2',
    },
    {
      roleId: 3,
      roleName: '角色名称3',
    },
  ]},
  'GET /api/store4user': {data:[
    {
      storeId: 1,
      storeName: '商户1',
    },
    {
      storeId: 2,
      storeName: '商户2',
    },
    {
      storeId: 3,
      storeName: '商户3',
    },
    {
      storeId: 4,
      storeName: '商户4',
    },
    {
      storeId: 5,
      storeName: '商户5',
    },
    {
      storeId: 6,
      storeName: '商户6',
    },
  ]},
};
